package com.qqtech.qquan.delivery.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.qqtech.core.frame.model.PageKit;
import com.qqtech.core.frame.model.PageOrder;
import com.qqtech.core.frame.model.Result;
import com.qqtech.core.frame.service.BaseService;
import com.qqtech.qquan.delivery.enums.DeliveryOrderDictTypeConstants;
import com.qqtech.qquan.delivery.model.DeliverySysDict;
import com.qqtech.qquan.delivery.model.DeliverySysDictQuery;
import com.qqtech.qquan.delivery.model.DeliverySysDictVo;
import com.qqtech.qquan.delivery.service.DeliverySysDictService;
import com.qqtech.web.context.BaseWebController;

@Controller
@RequestMapping(value = "/delivery/sys/dict")
public class DeliverySysDictController extends BaseWebController<DeliverySysDict, DeliverySysDictQuery> {
	@Resource
	private DeliverySysDictService deliverySysDictService;
    
	@Override
	protected BaseService<DeliverySysDict> getBaseService() {
		return deliverySysDictService;
	}
	
	/**
	 * 获取物流公司
	 * @return
	 */
	@RequestMapping(value = "/getLogistics")
	@ResponseBody
	public Result getLogistics() {
		Result result = new Result();
		DeliverySysDictQuery deliverySysDictQuery = new DeliverySysDictQuery();
		deliverySysDictQuery.setType(DeliveryOrderDictTypeConstants.DICTYPE_DELIVERY_DELIVERY_NAME);
		Pageable pageKit = new PageKit(1, 9999, new PageOrder(
				Direction.ASC, "remark"));
		List<DeliverySysDictVo> list = deliverySysDictService.queryList(deliverySysDictQuery, pageKit);
		Map<String, Object> map = new HashMap<String, Object>();
		String[] firstChar = {"A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"};
		for (String c : firstChar) {
			List<Map<String, Object>> dataList = new ArrayList<Map<String,Object>>();
			Map<String, Object> tmplate = new HashMap<String, Object>();
			tmplate.put("template", 0);
			tmplate.put("firstChar", c);
			dataList.add(tmplate);
			boolean flag = false;
			for (DeliverySysDictVo vo : list) {
				if (c.equals(vo.getRemark())) {
					Map<String, Object> tmplate2 = new HashMap<String, Object>();
					tmplate2.put("template", 1);
					tmplate2.put("name", vo.getName());
					dataList.add(tmplate2);
					tmplate2 = null;
					flag = true;
				}
			}
			if (flag) {
				map.put(c, dataList);
			}
			dataList = null;
		}
		result.put("logistics", map);
		return result;
	}
}
